package com.gang.music.system.controller;

import com.gang.music.common.IndexPageData;
import com.gang.music.core.shiro.ShiroUser;
import com.gang.music.core.toolbox.MenuUtils;
import com.gang.music.system.entity.Menu;
import com.gang.music.system.entity.User;
import com.gang.music.system.service.MenuService;
import com.gang.music.system.service.SongInfoService;
import com.gang.music.system.service.SongService;
import com.gang.music.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 后台管理首页
 *
 */
@RequestMapping("/system/main")
@Controller
public class MainController {
	
	@Autowired
	private MenuService menuService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private SongService songService;
	
	@Autowired
	private SongInfoService songInfoService;

	@RequestMapping(method=RequestMethod.GET)
	public String main(Map<String, Object> map, 
			HttpSession session) {
		
		ShiroUser shiroUser = (ShiroUser) session.getAttribute("shiroUser");
		List<Menu> menuList = menuService.findSystemMenu();
		List<Integer> userMenuIds = userService.findMenuIdsByUserId(shiroUser.getId());
		map.put("menuList", MenuUtils.getMenu(menuList, userMenuIds));
		return "admin/main/main";
	}
	
	@RequestMapping(value="/index", method=RequestMethod.GET)
	public String index(Map<String, Object> map) {
		
		List<User> todayRegisterUser = userService.findUserByTodayRegister();
		Long todayAmout = songInfoService.queryTodayAmout();
		Long totalSongNum = songService.queryTotalSongNum();
		Long totalUserNum = userService.queryTotalUserNum();
		IndexPageData data = new IndexPageData();
		data.setTodayAmout(todayAmout);
		data.setTodayRegisterUser((long)todayRegisterUser.size());
		data.setTotalSongNum(totalSongNum);
		data.setTotalUserNum(totalUserNum);
		map.put("data", data);
		return "admin/main/index";
	}
}
